package com.ruoyi.cloudpc.service.impl;

import com.ruoyi.cloudpc.domain.CloudAfterSale;
import com.ruoyi.cloudpc.domain.CloudOrder;
import com.ruoyi.cloudpc.mapper.CloudAfterSaleMapper;
import com.ruoyi.cloudpc.mapper.CloudOrderMapper;
import com.ruoyi.cloudpc.service.ICloudAfterSaleService;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 售后Service业务层处理
 *
 * @author Ten
 * @date 2025-09-13
 */
@Service
public class CloudAfterSaleServiceImpl implements ICloudAfterSaleService {
    @Autowired
    private CloudAfterSaleMapper cloudAfterSaleMapper;
    @Autowired
    private CloudOrderMapper cloudOrderMapper;
    /**
     * 查询售后
     *
     * @param id 售后主键
     * @return 售后
     */
    @Override
    public CloudAfterSale selectCloudAfterSaleById(Long id) {
        return cloudAfterSaleMapper.selectCloudAfterSaleById(id);
    }

    /**
     * 查询售后列表
     *
     * @param cloudAfterSale 售后
     * @return 售后
     */
    @DataScope(userAlias = "u")
    @Override
    public List<CloudAfterSale> selectCloudAfterSaleList(CloudAfterSale cloudAfterSale) {
        return cloudAfterSaleMapper.selectCloudAfterSaleList(cloudAfterSale);
    }

    /**
     * 新增售后
     *
     * @param cloudAfterSale 售后
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCloudAfterSale(CloudAfterSale cloudAfterSale) {
        CloudOrder cloudOrder = cloudOrderMapper.selectCloudOrderById(cloudAfterSale.getOrderId());
        cloudOrder.setAfterSale("1");
        if (cloudOrderMapper.updateCloudOrder(cloudOrder) != 1) {
            throw new RuntimeException("操作失败");
        }

        cloudAfterSale.setCreateByAndTime();
        return cloudAfterSaleMapper.insertCloudAfterSale(cloudAfterSale);
    }

    /**
     * 修改售后
     *
     * @param cloudAfterSale 售后
     * @return 结果
     */
    @Override
    public int updateCloudAfterSale(CloudAfterSale cloudAfterSale) {
        cloudAfterSale.setUpdateByAndTime();
        return cloudAfterSaleMapper.updateCloudAfterSale(cloudAfterSale);
    }

    /**
     * 批量删除售后
     *
     * @param ids 需要删除的售后主键
     * @return 结果
     */
    @Override
    public int deleteCloudAfterSaleByIds(Long[] ids) {
        return cloudAfterSaleMapper.deleteCloudAfterSaleByIds(ids);
    }

    /**
     * 删除售后信息
     *
     * @param id 售后主键
     * @return 结果
     */
    @Override
    public int deleteCloudAfterSaleById(Long id) {
        return cloudAfterSaleMapper.deleteCloudAfterSaleById(id);
    }
}
